<template>
  <!--财务信息 -->
  <div>
    <FormPanel
      ref="FormPanel"
      :list="initForm"
      :cols="3"
      :formType="formType"
      :labelWidth="150"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
      class="mtop"
    />
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import * as types from '@/api/cms-api/origin/originInfo/types';
import {phoneAndSolidify,email,systemCode,phoneAndSolidifyNew} from '@/utils/validate.js';
import vm from './bridge';

export default {
  mixins: [dictionary],
  model: {
    prop: 'pdata',
    event: 'click'
  },
  props: {
    pdata: {
      type: Object,
      default: () => ({})
    },
    formType: {
      type: String,
      required: true
    },
    formOptions: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      initForm: this.initFormFun()
    };
  },
  mounted() {
    //注册经销商类型变化时触发的事件
    vm.$on('changeDealerType', this.changeDealerType);
  },
  methods: {
    //初始化数据
    init() {
      this.initForm.forEach(x => {
        if (this.pdata[x.fieldName]) {
          x.initialValue = this.pdata[x.fieldName];
        }
      });
    },
    //表单验证通过后
    formChangeHandle(val) {
      this.pdata = Object.assign(this.pdata, val);
      //传递数据到父组件里
      this.$emit('click', this.pdata);
      //触发外部的事件，以标志着本表单已经准备完毕
      this.$emit('formchange', this.pdata);
    },
    //开放的提交方法
    async saveDialog() {
      let flag = true;
      let data = this.$refs.FormPanel.SUBMIT_FORM();
      console.log("data:",data);
      await this.$refs.FormPanel.$refs.form.validate().then(res => {
        console.log("res1:", res);
      }).catch(err => {
        console.log("err1:", err);
        flag = false;
      });
      console.log("flag:", flag);
      return flag;
    },
    //经销商类型变更时
    changeDealerType(value) {
      //判断是否是dle页面，是则使用dlr专用验证
      let suffix = this.formOptions.formType === types.DEALER_INFO_FORM_STATUS_SUBMISSION_DLR ? 'DLR' : '';
      //根据类型处理验证
      if (types.DEALER_INFO_DEALER_TYPE_4S === value) {
        //4S
        vm.changeForm(this.initForm, '4S' + suffix, false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_SINGLE_AFTER_SALE === value) {
        //单一售后
        vm.changeForm(this.initForm, 'SH' + suffix, false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_SINGLE_SALE === value) {
        //单一销售
        vm.changeForm(this.initForm, 'XS' + suffix, false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_DIRECT_SHOP === value) {
        //SPACE
        vm.changeForm(this.initForm, 'SPACE' + suffix, false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_NO_DIRECT_SHOP === value) {
        //RACE
        vm.changeForm(this.initForm, 'RACE' + suffix, false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER === value) {
        //大客户
        vm.changeForm(this.initForm, 'DKH' + suffix, false);
        vm.clearValidate(this.$refs.FormPanel);
      }
    },
    //表单数据
    initFormFun() {
      let bt = { required: true, message: '必填', trigger: ['change', 'blur'] };
      let btl = [];
      let yb = { pattern: /^[0-9][0-9]{5}$/, message: '请填写正确的邮编', trigger: 'blur' };

      return [
        {
          type: 'INPUT',
          label: '纳税人识别号',
          fieldName: 'taxpayerIdentityNumber',
          initialValue: '',
          maxlength: 20,
          rules: [
            bt,
            { validator: systemCode, trigger: 'blur'}
          ]
        },
        {
          type: 'INPUT',
          label: '公司开户行',
          fieldName: 'bank',
          initialValue: '',
          maxlength: 30,
          rules: btl,
        },
        {
          type: 'INPUT',
          label: '公司账户',
          fieldName: 'bankAccount',
          initialValue: '',
          maxlength: 20,
          rules: btl,
        },
        // {
        //   type: 'INPUT',
        //   label: '分账账号',
        //   fieldName: 'subAccount',
        //   initialValue: '',
        //   maxlength: 60,
        //   rules: [],
        // },
        {
          type: 'DATE',
          format: 'yyyy-MM-dd',
          label: '生效时间',
          fieldName: 'effectiveDate',
          rows: 1,
          initialValue: '',
          placeholder: '请输入',
          maxlength: 300,
          rules: btl,
        },
        {
          type: 'INPUT',
          label: '财务经理',
          fieldName: 'financeManager',
          initialValue: '',
          maxlength: 20,
          rules: btl,
        },
        {
          type: 'INPUT',
          label: '财务负责人联系电话',
          fieldName: 'financeManagerTel',
          initialValue: '',
          maxlength: 30,
          rules: [
            { validator: phoneAndSolidifyNew, trigger: 'blur'}
          ],
        },
        {
          type: 'INPUT',
          label: '发票邮箱',
          fieldName: 'financeManagerEmail',
          initialValue: '',
          maxlength: 30,
          rules: [
            { validator: email, trigger: 'blur'}
          ],
        },
        {
          type: 'INPUT',
          label: '发票邮寄地址',
          rows: 1,
          fieldName: 'billSendAddress',
          initialValue: '',
          maxlength: 100,
          rules: btl,
        },

      ];
    }
  }
};
</script>
